﻿/**
 * Copyright (c) 2009, Carlos Mirabella
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Carlos Mirabella nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 **/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DineroMail.Reporte
{
    public class Report
    {
        public enum EstadoReporteEnum
        {
            ParametrosInvalidos = 0,
            Correcto = 1,
            ValoresIncorrectos = 2
        }

        #region Variables
        private String email;
        private String account;
        private String pin;
        private String startDate;
        private String endDate;
        private String xml;
        private EstadoReporteEnum state;
        private List<Pay> pays;
        private List<Collection> collections;
        private List<Ticket> tickets;
        private List<Reception> receptions;
        private List<Retreat> retreats;
        private List<Credit> credits;
        private List<Debit> debits;

        #endregion
        #region Getters y Setters
        
        /// <summary>
        /// E-mail Recibido
        /// </summary>
        public String Email
        {
            get { return email; }
            set { email = value; }
        }
        /// <summary>
        /// Cuenta Recibida
        /// </summary>
        public String Account
        {
            get { return account; }
            set { account = value; }
        }
        /// <summary>
        /// Clave Recibida
        /// </summary>
        public String PIN
        {
            get { return pin; }
            set { pin = value; }
        }
        /// <summary>
        /// Fecha inicial Recibida
        /// </summary>
        public String StartDate
        {
            get { return startDate; }
            set { startDate = value; }
        }
        /// <summary>
        /// Fecha final Recibida
        /// </summary>
        public String EndDate
        {
            get { return endDate; }
            set { endDate = value; }

        }
        /// <summary>
        /// Flag
        /// </summary>
        public String Xml
        {
            get { return xml; }
            set { xml = value; }

        }
        /// <summary>
        /// Estado Recibido
        /// </summary>
        public EstadoReporteEnum State
        {
            get { return state; }
            set { state = value; }
        }

        /// <summary>
        /// Botones de Pago
        /// </summary>
        public List<Pay> Pays
        {
            get { return pays; }
            set { pays = value; }
        }
        /// <summary>
        /// Tarjetas de Cobranza y Codigos de Barra
        /// </summary>
        public List<Collection> Collections
        {
            get { return collections; }
            set { collections = value; }
        }
        /// <summary>
        /// Boletas de Pago
        /// </summary>
        public List<Ticket> Tickets
        {
            get { return tickets; }
            set { tickets = value; }
        }
        /// <summary>
        /// Recepciones de Dinero
        /// </summary>
        public List<Reception> Receptions
        {
            get { return receptions; }
            set { receptions = value; }
        }
        /// <summary>
        /// Retiros de Fondos
        /// </summary>
        public List<Retreat> Retreats
        {
            get { return retreats; }
            set { retreats = value; }
        }
        /// <summary>
        /// Creditos
        /// </summary>
        public List<Credit> Credits
        {
            get { return credits; }
            set { credits = value; }
        }
        /// <summary>
        /// Debitos
        /// </summary>
        public List<Debit> Debits
        {
            get { return debits; }
            set { debits = value; }
        }
        #endregion

        public Report()
        {
            
        }
        

        public static Report ParseXml(XmlDocument xmlDoc)
        {
            XmlNode node = xmlDoc.DocumentElement;
            Report reporte = new Report();
            if (node.Name.Equals("Report"))
            {
                foreach (XmlNode child in node.ChildNodes)
                {
                    if (child.Name.Equals("Email"))
                        reporte.Email=child.FirstChild.Value;
                    else if (child.Name.Equals("Acount"))
                        reporte.Account=child.FirstChild.Value;
                    else if (child.Name.Equals("Pin"))
                        reporte.PIN = child.FirstChild.Value;
                    else if (child.Name.Equals("StartDate"))
                        reporte.StartDate = child.FirstChild.Value;
                    else if (child.Name.Equals("EndDate"))
                        reporte.EndDate = child.FirstChild.Value;
                    else if (child.Name.Equals("XML"))
                        reporte.Xml = child.FirstChild.Value;
                    else if (child.Name.Equals("State"))
                        reporte.State =(EstadoReporteEnum) Enum.Parse(typeof(EstadoReporteEnum),child.FirstChild.Value);
                    else if (child.Name.Equals("Pays"))
                        reporte.Pays = GetPays(child);
                    else if (child.Name.Equals("Collections"))
                        reporte.Collections = GetCollections(child);
                    else if (child.Name.Equals("Tickets"))
                        reporte.Tickets = GetTicktes(child);
                    else if (child.Name.Equals("Receptions"))
                        reporte.Receptions = GetReceptions(child);
                    else if (child.Name.Equals("Retreats"))
                        reporte.Retreats = GetRetreats(child);
                    else if (child.Name.Equals("Credits"))
                        reporte.Credits = GetCredits(child);
                    else if (child.Name.Equals("Debits"))
                        reporte.Debits = GetDebits(child);
                }

            }
            return reporte;
        }

        private static List<Pay> GetPays(XmlNode parent)
        {
            List<Pay> pays = new List<Pay>();
            foreach (XmlNode child in parent.ChildNodes)
            {
                Pay p = Pay.GetPay(child);
                if (p!=null)
                    pays.Add(p);
            }
            return pays;
        }
        private static List<Collection> GetCollections(XmlNode parent)
        {
            List<Collection> colls = new List<Collection>();
            foreach (XmlNode child in parent.ChildNodes)
            {
                Collection c = Collection.GetCollection(child);
                if (c != null)
                    colls.Add(c);
            }
            return colls;
        }
        private static List<Ticket> GetTicktes(XmlNode parent)
        {
            List<Ticket> ticktes = new List<Ticket>();
            foreach (XmlNode child in parent.ChildNodes)
            {
                Ticket t = Ticket.GetTicket(child);
                if (t != null)
                    ticktes.Add(t);
            }
            return ticktes;
        }
        private static List<Reception> GetReceptions(XmlNode parent)
        {
            List<Reception> receptions = new List<Reception>();
            foreach (XmlNode child in parent.ChildNodes)
            {
                Reception r = Reception.GetReception(child);
                if (r != null)
                    receptions.Add(r);
            }
            return receptions;
        }
        private static List<Retreat> GetRetreats(XmlNode parent)
        {
            List<Retreat> retreats = new List<Retreat>();
            foreach (XmlNode child in parent.ChildNodes)
            {
                Retreat r = Retreat.GetRetreat(child);
                if (r != null)
                    retreats.Add(r);
            }
            return retreats;
        }
        private static List<Credit> GetCredits(XmlNode parent)
        {
            List<Credit> credits = new List<Credit>();
            foreach (XmlNode child in parent.ChildNodes)
            {
                Credit c = Credit.GetCredit(child);
                if (c != null)
                    credits.Add(c);
            }
            return credits;
        }
        private static List<Debit> GetDebits(XmlNode parent)
        {
            List<Debit> debits = new List<Debit>();
            foreach (XmlNode child in parent.ChildNodes)
            {
                Debit d = Debit.GetDebit(child);
                if (d != null)
                    debits.Add(d);
            }
            return debits;
        }



        /// <summary>
        /// Url para realizar la Consulta
        /// </summary>
        public static String UrlPost
        {
            get
            {
                return Properties.Settings.Default.ConsultaUrl;
            }
        }
        public static String BuildQueryStringPost(DateTime start, DateTime end)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Email=");
            sb.Append(Properties.Settings.Default.Email);
            sb.Append("&Acount=");
            sb.Append(Properties.Settings.Default.NumeroCuenta);
            sb.Append("&Pin=");
            sb.Append(Properties.Settings.Default.PinNumber);
            sb.Append("&StartDate=");
            sb.Append(start.Year.ToString()+ start.Month.ToString() + start.Day.ToString());
            sb.Append("&EndDate=");
            sb.Append(end.Year.ToString() + end.Month.ToString() + end.Day.ToString());
            sb.Append("&XML=1");
            return sb.ToString();
        }
    }
}

